/*!
 Temelia - AVL tree performance tests.

 Copyright (C) 2008 Ceata (http://cod.ceata.org/proiecte/temelia).

 @author Dascalu Laurentiu

 This program is free software; you can redistribute it and
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 3
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "include/performance.hpp"
#include "include/testutil.hpp"

extern "C"
{
#include <temelia/avl_tree.h>
#include <temelia/common.h>
#include <temelia/binary_tree.h>
}

static avl_tree_t __temelia_avl_tree_insert(int n)
{
	avl_tree_t avl_tree;

	avl_tree = avl_tree_new(NULL);

	for (int i = 0; i < n; i++)
		avl_tree_insert(&avl_tree, default_collection[i], int_compare, NULL, 1);

	return avl_tree;
}

static void temelia_avl_tree_insert(int n)
{
	avl_tree_delete(__temelia_avl_tree_insert(n));
}

static void temelia_avl_tree_search(int n)
{
	avl_tree_t avl_tree;

	avl_tree = __temelia_avl_tree_insert(n);

	for (int i = 0; i < n; i++)
		avl_tree_search(avl_tree, default_collection[i], int_compare, NULL);

	avl_tree_delete(avl_tree);
}

static void temelia_avl_tree_remove(int n)
{
	avl_tree_t avl_tree;

	avl_tree = __temelia_avl_tree_insert(n);

	for (int i = 0; i < n; i++)
		avl_tree_remove(&avl_tree, default_collection[i], int_compare, NULL, 1);

	avl_tree_delete(avl_tree);
}

void TemeliaPerformance::pt_avl(void)
{
	TestUnit testUnit("avl_tree");

	GENERATE_TEMELIA_TEST(testUnit, "insert", avl_tree_insert);
	GENERATE_TEMELIA_TEST(testUnit, "search", avl_tree_search);
	GENERATE_TEMELIA_TEST(testUnit, "remove", avl_tree_remove);

	testUnit.write(fout);
}
